package com.dose.test.db.cryp;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.util.UUID;

import javax.crypto.Cipher;

public class Des {
	private static String strDefaultKey = "key";

	private Cipher encryptCipher = null;

	private Cipher decryptCipher = null;

	public String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	public byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes("utf-8");
		int iLen = arrB.length;

		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i = i + 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}

	public Des() throws Exception {
		this(strDefaultKey);
	}

	public Des(String strKey) throws Exception {
		setKey(strKey);
	}
	
	public void setKey(String strKey) throws UnsupportedEncodingException, Exception{
		Key key = getKey(strKey.getBytes("utf-8"));

		encryptCipher = Cipher.getInstance("DES");
		encryptCipher.init(Cipher.ENCRYPT_MODE, key);

		decryptCipher = Cipher.getInstance("DES");
		decryptCipher.init(Cipher.DECRYPT_MODE, key);
	}

	public byte[] encrypt(byte[] arrB) throws Exception {
		return encryptCipher.doFinal(arrB);
	}

	public String encrypt(String strIn) throws Exception {
		if (strIn == null) {
			strIn = "";
		}
		return byteArr2HexStr(encrypt(strIn.getBytes("utf-8")));
	}
	
	public String encrypt(String key, String strIn) throws Exception {
		this.setKey(key);
		if (strIn == null) {
			strIn = "";
		}
		return byteArr2HexStr(encrypt(strIn.getBytes("utf-8")));
	}

	public byte[] decrypt(byte[] arrB) throws Exception {
		return decryptCipher.doFinal(arrB);
	}

	public String decrypt(String strIn) throws Exception {
		return new String(decrypt(hexStr2ByteArr(strIn)));
	}
	
	public String decrypt(String key, String strIn) throws Exception {
		this.setKey(key);
		return new String(decrypt(hexStr2ByteArr(strIn)));
	}

	private Key getKey(byte[] arrBTmp) throws Exception {
		byte[] arrB = new byte[8];

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}

		Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

		return key;
	}

	public static void main(String[] argv) throws Exception{
		Des en = new Des();
		String encode = en.encrypt("wifi","你好");
		String decode = en.decrypt("wifi",encode);
		System.out.println(encode);
		System.out.println(decode);
		
		en = new Des();
		encode = en.encrypt("你好");
		decode = en.decrypt(encode);
		System.out.println(encode);
		System.out.println(decode);
		
		en = new Des();
		encode = en.encrypt("12345678","ABCDEFGH");
		decode = en.decrypt(encode);
		System.out.println(encode);
		System.out.println(decode);
		
		String uuid = UUID.randomUUID().toString();
		System.out.println(uuid);
		System.out.println(System.getProperty("file.encoding"));
	}
}
